﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DataAccessLayer;
using BussinessLayer;

namespace QLBaoHanh.QLDanhMuc
{
    public partial class frmThietBiKhoDetail : Form
    {
        private bool _isEditForm;
        private ThietBiKho _thietBiKho;
        private int _thietBiKhoId;
        private frmDsThietBiKho _callerForm;

        public int ThietBiKhoId
        {
            set
            {
                _thietBiKhoId = value;
                //reload ThietBiKho
                _thietBiKho = ThietBiKhoBO.getInstance.GetThietBiKhoById(_thietBiKhoId);
                RefreshForm();
            }
        }

        public bool IsEditForm
        {
            set
            {
                _isEditForm = value;
                if (!_isEditForm)
                    FillFormThietBiKho();
            }
        }

        public frmDsThietBiKho CallerForm
        {
            get { return _callerForm; }
            set { _callerForm = value; }
        }

        public frmThietBiKhoDetail()
        {
            InitializeComponent();
            _isEditForm = false;
            _thietBiKhoId = 0;
            _thietBiKho = null;
        }

        private void FillFormThietBiKho()
        {
            // Fill combobox nha cung cap
            cboNhaCungCap.DataSource = NhaCungCapBO.getInstance.GetAllNhaCungCap();
            cboNhaCungCap.DisplayMember = "Ten";
            cboNhaCungCap.ValueMember = "Id";
            // Fill combobox nha san xuat
            cboNhaSanXuat.DataSource = NhaSanXuatBO.getInstance.GetAllNhaSanXuat();
            cboNhaSanXuat.DisplayMember = "Ten";
            cboNhaSanXuat.ValueMember = "Id";
            if (_isEditForm)
            {
                txtMaTbi.Text = _thietBiKho.Ma;
                txtName.Text = _thietBiKho.Ten;
                txtSoLuong.Text = _thietBiKho.SoLuong.ToString();
                txtDonVi.Text = _thietBiKho.DonVi;
                //for (int i = 0; i < cboNhaCungCap.Items.Count; i++ )
                //{
                //    if(cboNhaCungCap.Items[i].value )
                //}
                cboNhaCungCap.SelectedValue = (object)_thietBiKho.NCCId;
                cboNhaSanXuat.SelectedValue = (object)_thietBiKho.NSXId;
            } 
            else
            {
                this.Text = "Thêm thiết bị kho";
                txtTitle.Text = "Thêm mới thiết bị kho";
                txtMaTbi.Text = ThietBiKhoBO.getInstance.GenerateMaThietBiKho();
                txtName.Text = "";
                txtSoLuong.Text = "";
            }
        }

        private bool IsDataHasChanged()
        {
            bool hasChange = false;
            //only check in case edit
            if(_isEditForm && _thietBiKho != null)
            {
                if (  txtName.Text != _thietBiKho.Ten)
                {
                    hasChange = true;
                }
            }
            else
            {
                if (txtName.Text != "" || txtSoLuong.Text != "" )
                {
                    hasChange = true;
                }
            }
            return hasChange;
        }

        private bool SaveThietBiKho()
        {
            if (ValidateFormData())
            {
                if (IsDataHasChanged())
                {
                    if (_isEditForm)
                    {
                        //update  thiet bi kho
                        _thietBiKho.Ten = txtName.Text.Trim();
                        _thietBiKho.SoLuong = Convert.ToInt32(txtSoLuong.Text.Trim());
                        _thietBiKho.DonVi = txtDonVi.Text.Trim();
                        _thietBiKho.NCCId = (int)cboNhaCungCap.SelectedValue;
                        _thietBiKho.NSXId = (int)cboNhaSanXuat.SelectedValue;
                        ThietBiKhoBO.getInstance.UpdateThietBiKho(_thietBiKho);
                    } 
                    else
                    {
                        //insert thiet bi kho
                        _thietBiKho = new ThietBiKho();
                        _thietBiKho.Ma = txtMaTbi.Text.Trim();
                        _thietBiKho.Ten = txtName.Text.Trim();
                        //_thietBiKho.Mobile = txtSoLuong.Text.Trim();
                        //_thietBiKho.DiaChi = txtAddress.Text.Trim();
                        ThietBiKhoBO.getInstance.InsertThietBiKho(_thietBiKho);
                        _callerForm.RefreshForm();
                    }
                }
                return true;
            }
            else
            {
                return false;
            }
        }

        private bool ValidateFormData()
        {
            //hien thi message du lieu ko valid
            return true;
        }

        private void RefreshForm()
        {
            FillFormThietBiKho();
        }

        private void frmThietBiKhoDetail_Load(object sender, EventArgs e)
        {
            //Neu la edit thi load form edit khong thi load form them moi
            if (_isEditForm)
            {
                if(_thietBiKho == null)
                {
                    return;
                }
            }
 
            FillFormThietBiKho();
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            if (SaveThietBiKho())
            {
                this.Close();
            }
        }

        private void btnHuy_Click(object sender, EventArgs e)
        {
            RefreshForm();
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            if(IsDataHasChanged())
            {
                if (MessageBox.Show("Dữ liệu có sự thay đổi, Bạn có muốn lưu không?"
                , "Đóng", MessageBoxButtons.YesNo, MessageBoxIcon.Information) == DialogResult.Yes)
                {
                    //save data
                    if (SaveThietBiKho())
                    {
                        this.Close();
                    }
                }
                else
                {
                    //close form
                    this.Close();    
                }
            }
            else
            {
                //close form
                this.Close();
            }
        }
    }
}
